import { Controller, Get, Post, Req, Res } from '@nestjs/common';
import { VerificationCodeService } from './verification-code.service';
import { CreateVerificationCodeDto } from './dto/create-verification-code.dto';
import { UpdateVerificationCodeDto } from './dto/update-verification-code.dto';

import * as svgCaptcha from 'svg-captcha';

@Controller('verification-code')
export class VerificationCodeController {
  constructor(
    private readonly verificationCodeService: VerificationCodeService,
  ) {}

  @Get('code')
  createCaptcha(@Req() req, @Res() res) {
    const captcha = svgCaptcha.create({
      size: 4, //生成几个验证码
      fontSize: 50, //文字大小
      width: 100, //宽度
      height: 34, //高度
      background: '#cc9966', //背景颜色
    });
    req.session.code = captcha.text; //存储验证码记录到 session
    res.type('image/svg+xml');
    res.send(captcha.data);
  }

  @Post('login')
  login(@Req() req, @Res() res) {
    const state =
      req.session.code.toLocaleLowerCase() === req.body.code.toLocaleLowerCase()
        ? true
        : false;
    res.send({
      code: state ? 0 : 1,
      msg: state ? '验证码正确' : '验证码错误',
    });
  }
}
